<script lang="ts" setup>
import HomeRequest from '@/request/homeRequest';
import { useAppStore } from '@/store';
import to from '@/utils/to';

const activityDetails = ref<IObtainActivityList | undefined>(undefined);
const btnList = ref<IObtainActivityButtonListRes[]>([]);
const store = useAppStore();

onLoad(async (options) => {
  if (!options) return;
  activityDetails.value = JSON.parse(decodeURIComponent(options.data));
  handleActions(1, activityDetails.value!.activityId);
  const [err, res] = await to(HomeRequest.ObtainActivityButtonList({ activityId: activityDetails.value!.activityId }));
  if (err) return;
  btnList.value = res.data;
});

// 活动浏览量点赞与不喜欢功能状态操作
const handleActions = async (type: number, id: number) => {
  await to(HomeRequest.ActivityActionsUpdate({ type, id }));
};

// 活动产品点击事件处理
const handleActivityProductClick = async (data: IObtainActivityButtonListRes) => {
  const [err, res] = await to(HomeRequest.ObtainProductThroughButton({ activityServerId: data.activityServerId, beeperId: store.$state.beeperId, hasLoading: true }));
  if (err) return;
  const pages = getCurrentPages();
  const previousPage = pages[pages.length - 2];
  if (previousPage && typeof previousPage.$vm.handleProdPayTypeOperation === 'function') {
    previousPage.$vm.handleProdPayTypeOperation(res.data, activityDetails.value!.buyWay, data);
  }
};
</script>
<template>
  <view class="bg-white h-screen c-pd" :style="{ padding: useAppStore().$state.systemInfo.safeAreaInsets!.bottom }" v-if="activityDetails">
    <view class="flex justify-between items-center mb-12px c-text-color-grey c-text-12">
      <view class="flex items-center">浏览数<i class="i-mdi:eye mx-4px"></i>{{ activityDetails.viewNums }}</view>
      <view class="flex items-center">
        <view class="flex items-center" @click="handleActions(1, activityDetails.activityId)"><i class="i-mingcute:thumb-up-2-fill" :class="activityDetails.upNums < 0 ? 'text-red' : ''"></i>{{ Math.abs(activityDetails.upNums) }}</view>
        <view class="flex items-center" :class="activityDetails.downNums < 0 ? 'text-red' : ''" @click="handleActions(1, activityDetails.activityId)"><i class="i-mingcute:thumb-down-2-fill ml-8px"></i>{{ Math.abs(activityDetails.downNums) }}</view>
      </view>
    </view>
    <UvParse :content="activityDetails.content" containerStyle="padding-bottom: 30px" />

    <view class="mt-12px" v-for="item in btnList" :key="item.activityServerId">
      <van-button type="primary" block @click="handleActivityProductClick(item)">{{ item.btnName }} </van-button>
    </view>
  </view>

  <van-empty v-else description="暂无数据" />
</template>
